/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi Installer.

    Maxsi Installer is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Installer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Installer.  If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiInstallerContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiEngineContributors.h. For more information please see
	MaxsiEngineContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]

	MaxsiInstaller.exe
	The main installing technology utilized in the powerful Maxsi Distribution
	platform.

	PageWhereToInstall.cpp
	Where should this product be installed?

****************************************************************************/

#include "MaxsiEngine.h"
#include "MaxsiDistributionLink.h"
#include "Main.h" 
#include "UI.h" 
#include "Thread.h"
#include "Data.h"


char* DetectSteamFolder( void )
{
	char* SteamFolderPath = 0;

	char lszValue[MAX_PATH];

	HKEY hKey;
	LONG returnStatus;

	size_t dwType	=	REG_SZ;
	size_t dwSize	=	MAX_PATH;

	returnStatus = RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Valve\\Steam\\", 0L,  KEY_READ, &hKey);

	if (returnStatus == ERROR_SUCCESS)
	{
		returnStatus = RegQueryValueExA(hKey, "SteamPath", NULL, (LPDWORD)&dwType,(LPBYTE)&lszValue, (LPDWORD)&dwSize);

		if (returnStatus == ERROR_SUCCESS)
		{
			SteamFolderPath = new char[dwSize+2];
			SteamFolderPath[dwSize] = 0; // Lets null-terminate!
			memcpy(SteamFolderPath,lszValue,dwSize);
			if (SteamFolderPath[dwSize-2] != '/' &&
				SteamFolderPath[dwSize-2] != '\\' )
			{
				SteamFolderPath[dwSize-1]		= '/';
				SteamFolderPath[dwSize+0]		= 0;
			}
		}

		RegCloseKey(hKey);	
	}

	
	return SteamFolderPath;
}

bool PageWhereToInstall::InitializeForm(ME_Window* Parent)
{
	BaseClass::InitializeForm(Parent);

	RECT ClientSpace;
	GetClientRect (WindowHWND, &ClientSpace);


	if ( !InstallPath )
	{
		InstallPath = DetectSteamFolder();
	}

	char*	SteamPathSTR	=	BuildString(1,InstallPath);
	SteamPath	=	CreateTextA(WindowHWND,SteamPathSTR,0,0,0,0);
	delete[] SteamPathSTR;
	//SteamPath	=	CreateTextA(WindowHWND,0,0,0,0,0);
	Previous	=	CreateButton(WindowHWND,_MESTR("Previous"),0,0,0,0);
	Next		=	CreateButton(WindowHWND,_MESTR("Next"),0,0,0,0);
	char* TextSTR = BuildString(3,
	"First of all we'll need to know where Steam is installer on your computer. "
	"If you do not have Steam installed, please go to www.steamgames.com and download it "
	"and purchase ",MapPackFor,"."
	"\n\nSteam was detected in the following folder, please correct it if it is not the correct folder.")
	;
	Text		=	CreateLabelA(WindowHWND,TextSTR,0,0,0,0);
	delete[] TextSTR;

	NewResolution((WORD)ClientSpace.right,(WORD)ClientSpace.bottom);

	return true;
}

bool PageWhereToInstall::RenderForm(HWND hWnd)
{
	return	BaseClass::RenderForm(hWnd);
}

LRESULT PageWhereToInstall::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	if (!Initialized) { return 0; }

	if ( message == WM_COMMAND )
	{
        switch(LOWORD(wParam)) 
        {
            case BN_CLICKED:
			{
				HWND ClickedButton = (HWND)lParam;
				if (ClickedButton == Previous)
				{
					ReplaceSelfWith( (bLicense) ? (MaxsiUI_Form*) new PageAgreementProduct : new PageAgreementMaxsiDistribution );
				}
				else if ( ClickedButton == Next )
				{
					ReplaceSelfWith( (MaxsiUI_Form*) new PageSteamUsername );
				}
			}				
            break;
			default:
			{
				return BaseClass::WindowProc(hWnd,message,wParam,lParam);
			}
		}
	}
	else
	{
		return BaseClass::WindowProc(hWnd,message,wParam,lParam);
	}
    return (0);

}


bool PageWhereToInstall::TerminateForm()
{
	// Avoid calling this function twice, or worse,
	// if one of the functions called here invokes another
	// windows message that eventually leads to this point of code again.
	if (!Initialized) { return false; }
	Initialized	=	false;

	if ( InstallPath ) { delete[] InstallPath; InstallPath = 0; } // Clean up first, then reset
	// First save the path!
    WORD PathLen = (WORD)SendMessageA(SteamPath,EM_LINELENGTH,(WPARAM) 0,(LPARAM) 0);
	InstallPath = new char[PathLen+sizeof(WORD)];
	memset(InstallPath,0,(size_t)PathLen+sizeof(WORD));
	memcpy(InstallPath,&PathLen,sizeof(WORD));
		
	SendMessageA(SteamPath,EM_GETLINE,(WPARAM)0,(LPARAM)InstallPath);

	if (InstallPath[PathLen-1] != '/' &&
		InstallPath[PathLen-1] != '\\' )
	{
		InstallPath[PathLen]		= '/';
		InstallPath[PathLen+1]		= 0;
	}

	// It appears that working with unicode and ascii sometimes messes horribly up,

	DestroyWindow(Previous);
	DestroyWindow(Next);
	DestroyWindow(SteamPath);
	DestroyWindow(Text);

	// If we are here, we are safe. Call the parent and ask him to continue our work.
	// Make sure he sets Initialized to false afterwards.
	Initialized	=	true;
	BaseClass::TerminateForm();
	return true;
}

bool PageWhereToInstall::NewResolution( WORD Width, WORD Height)
{
	BaseClass::NewResolution(Width,Height);

	// Move the window, validate the Window, redraw the window	
	MoveWindow(Previous,Width-2*125,Height-2*24,100,24,TRUE);
	MoveWindow(Next,Width-1*125,Height-2*24,100,24,TRUE);
	MoveWindow(SteamPath,25,155,Width-25-25,24,TRUE);
	MoveWindow(Text,25,60,Width-25-25,155-60,TRUE);

	return true;
}